package com.hktf.uilib.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;

import androidx.annotation.FloatRange;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.hktf.uilib.R;

public class DownloadButton extends View {

    private Paint paint;
    private int blueColor;
    private int whiteColor;
    private float baseLine;
    private RectF rectF;
    private String statusText;
    private float progress;

    public DownloadButton(Context context) {
        this(context, null);
    }

    public DownloadButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DownloadButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public DownloadButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        blueColor = getResources().getColor(R.color.blue_ecu);
        whiteColor = getResources().getColor(R.color.white);
        int textSize = getResources().getDimensionPixelOffset(R.dimen.tv_size_s);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(textSize);
        paint.setColor(blueColor);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rectF = new RectF(1, 1, w - 1, h - 1);
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        baseLine = h / 2 + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
    }

    private boolean isDownload;//是否下载完成
    private boolean isStop=true;

    public boolean isStop() {
        return isStop;
    }

    public void setStop(boolean stop) {
        isStop = stop;
    }

    public boolean isDownload() {
        return isDownload;
    }

    public void setDownload(boolean download) {
        isDownload = download;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(isStop){
            progress=0;
        }
        if(isDownload){
            progress=1;
        }

        float height = rectF.height();
        float width = rectF.right;
        if (height <= 0 || width <= 0) {
            return;
        }
        paint.setShader(null);
        paint.setColor(blueColor);

        float radius = height / 2;
        if (progress == 1) {
            paint.setStyle(Paint.Style.FILL);
        } else {
            paint.setStyle(Paint.Style.STROKE);
        }
        canvas.drawRoundRect(rectF, radius, radius, paint);
        paint.setStyle(Paint.Style.FILL);

        if (progress > 0) {
            LinearGradient progressGradient = new LinearGradient(0, 0, width, 0,
                    new int[]{blueColor, Color.TRANSPARENT},
                    new float[]{progress, 0},//两种颜色占的比重
                    LinearGradient.TileMode.CLAMP);
            paint.setShader(progressGradient);
            canvas.drawRoundRect(rectF, radius, radius, paint);
            paint.setShader(null);
        }
        if (TextUtils.isEmpty(statusText)) {
            return;
        }
//        LogMds.i("cdz","isDownload="+isDownload);
//        LogMds.i("cdz","progress="+progress+"....this="+statusText+"....this="+this);

        rectF.right = width * progress;
        float textWidth = paint.measureText(statusText);
        float textLeft = width / 2 - textWidth / 2;
        float textRight = width / 2 + textWidth / 2;
        if (rectF.right >= textRight) {//进度完全覆盖了文字，文字不用计算进度，全部显示白色
            paint.setColor(whiteColor);
        } else if (rectF.right > textLeft) {//进度覆盖了文字，但是没有完全覆盖，计算文字进度
            float textProgress = (rectF.right - textLeft) / textWidth;
            LinearGradient textGradient = new LinearGradient(textLeft, 0, textRight, 0,
                    new int[]{whiteColor, blueColor},
                    new float[]{textProgress, 0},
                    LinearGradient.TileMode.CLAMP);
            paint.setShader(textGradient);
        }
        canvas.drawText(statusText, width / 2, baseLine, paint);
        rectF.right = width;
    }

    public void setProgress(@FloatRange(from = 0.0f, to = 1.0f) float progress) {
        this.progress = progress/100;
//        LogMds.i("cdz","progress1="+progress+"......this="+this);
        statusText=(int)progress+"%";
        invalidate();
    }

    public void setText(@StringRes int resid) {
        statusText = getResources().getString(resid);
        invalidate();
    }
    public void setText(String text) {
        statusText = text;
        invalidate();
    }

    public void setProgressAndText(@FloatRange(from = 0.0f, to = 1.0f) float progress, @StringRes int resid) {
        this.progress = progress;
        statusText = getResources().getString(resid);
        invalidate();
    }
}

